(function($, window, document, undefined){
	//SPG SECONDARY PROMOS PLUGIN
	var SPGPromo = {
		init: function(options, elem){
			var self = this;
			self.elem = elem;
			self.$elem = $( elem );
			self.options = $.extend( {}, $.fn.showSPGPromos.options, options );
			
			if(self.options.append){
				if(self.options.message1){
					self.showMessage1();
				}
				if(self.options.message2){
					self.showMessage2();
				}
				if(self.options.message3){
					self.showMessage3();
				}
				self.showSPGCard();
				if(self.options.activity){
					self.showActivity();
				}
				
			}else{
				if(self.options.activity){
					self.showActivity();
				}
				self.showSPGCard();
				if(self.options.message3){
					self.showMessage3();
				}
				if(self.options.message2){
					self.showMessage2();
				}
				if(self.options.message1){
					self.showMessage1();
				}
			}
			
			if(!self.options.upcomingStays){
				self.hideUpcomingStays();
			}
			
			$(".memmberInfo").insertAfter(".welcomeSearch");
			$(".upcomingStay").insertAfter(".memmberInfo");
		},
		
		showActivity: function(){
			var self = this;
			self.addPromo($(".myActivity"));
		},
		
		showSPGCard: function(){
			var self = this;
			self.addPromo($(".spgCard"));
		},
		
		showMessage1: function(){
			var self = this;
			self.addPromo($(".message1"));
			self.getLink(".message1");
		},
		
		showMessage2: function(){
			var self = this;
			self.addPromo($(".message2"));
			self.getLink(".message2");
		},
		
		showMessage3: function(){
			var self = this;
			self.addPromo($(".message3"));
			self.getLink(".message3");
		},
		
		hideUpcomingStays: function(){
			$(".upcomingStay").hide();
		},
		
		addPromo: function(promo){
			var self = this;
			if(self.options.append){
				self.$elem.append(promo);
			}else{
				self.$elem.prepend(promo);
			}
		},
		
		getLink: function(className){
			var self = this;
			
			if($(className + ' p').has('a').length > 0){
				var element = self.$elem.find(className + ' p a');
				var link = element[0].href;
				var linkCopy = element.html();
				var parent = element.parent();
				var replacedElement = element.replaceWith(linkCopy);
				var html = $(className).html();
				$(className).empty();
				
				$("<a></a>", {
					class: "link",
					href: link,
					html: html
				}).appendTo(className);
			}else{
				return;
			}
		}
	}
	
	$.fn.showSPGPromos = function(options){
		return this.each(function() {
			var spgPromo = Object.create( SPGPromo );
			
			spgPromo.init( options, this );

			$.data( this, 'showSPGPromos', spgPromo );
		});
	}
	
	$.fn.showSPGPromos.options = {
			message1: false,  //if true, displays message 1
			message2: false,  //if true, displays message 2
			message3: false,  //if true, displays message 3
			activity: false,  //if true, displays recent activity
			upcomingStays: false, //if true, displays upcoming stays module
			append: false, //if true, prepends tiles, otherwise, appends them
			onComplete: null //function to execute after complete
	};
	
})(jQuery, window, document);